﻿//
// Created by 50177 on 2019/7/8.
//

#ifndef OCTGUI_PROTOCOL_H
#define OCTGUI_PROTOCOL_H

#include "include/Communication/Response.h"
#include "include/Communication/Request.h"
//#include "include/OCTDevice/OctMotor.h"
#include "include/commonconst.h"

#include <QThread>
#include <QMutex>
#include <QWaitCondition>
#include <QtCore/qobject.h>
#include <QtSerialPort/QSerialPort>
#include <QMutex>
#include <QWaitCondition>
#include <atomic>

#include "include/Widget/Setting.h"

class Protocol : public QObject {
    Q_OBJECT
public:
    static Protocol *getInstance();
    static void connectPort(QString & portname);
    static void stop();
    void reconnect();
    void seMaxWaitTime(IN int delay);
    void clearBuffer();
    //--added by qinyaning.202007131402
    Response &send(IN const Request &request);
    //------
public slots:
    void init();
    void destroy();
    //--added by qinyaning.202007131411
    void sendSlot(const Request& request);
    //----
    //--added by qinyaning 202007131517
    void readSlot();
    //--
    void serialPortError(QSerialPort::SerialPortError error);
signals:
    //--added by qinyaning.202007131409
    void sendRequest(const Request& request);
    void onReadData(const Response& response);
    //----
    void SigShowBvError( int errorCode, int deviceType);//added by qinyaning 202008071520
    void onInit();
    //void SigMotorPosesGetTimeOutStatus(int index, int status);//added by qinyaning 202009151418
public:
    Protocol();
    ~Protocol();
    quint8 normalize(IN quint8 val, IN quint8 min, IN quint8 max);
    //--added by qinyaning. 202007130940 b
    bool createMirrorAxisFastLengthControlRequest(IN const Request &request);
    bool createMirrorAxisSlowLengthControlRequest(IN const Request &request);
    bool createMirrorModeControlRequest(IN const Request &request);
    bool createMirrorResetRequest(IN const Request &request);
    bool createOLEDRequest(IN const Request &request);
    bool createSLDRequest(IN const Request &request);
    inline void initRequestBuff();
    bool createQueryBoardVersionRequest(IN const Request &request);
	bool createEyeLightRequest(IN const Request &request);
    
	//bool createMotorAddress(IN const OctMotor &motor);
 //   bool createMoveRelRequest(IN const OctMotor &motor, IN OctMotor::Direction direction, IN double val);
 //   bool createMoveAbsRequest(IN const OctMotor &motor, IN double val);
 //   bool createQueryPositionRequest(IN const OctMotor &motor);
 //   bool createStopRequest(IN const OctMotor &motor);
 //   bool createMoveInfiniteRequest(IN const OctMotor &motor, IN OctMotor::Direction direction);
    //--added by qinyaning.202007130940 e
    //void sendGetMotorTimeoutStatus(char motorType1, char motorType2);//added by qinyaning 202009151427

private:
    static Protocol *instance;
    static QThread *worker;
    QSerialPort *serialPort = nullptr;
    char *readBuff = nullptr;
    int readCount=0;
    QString portName;
    std::atomic<bool> available;				//原子操作库
    QMutex mutexReq;
    QMutex mutexResp;
    QWaitCondition waitRespCond;
    Response *currentResponse = nullptr;
    int _delayTime = 500;
    //--added by qinyaning. 202007130931
    char* _requestCmdBuff = nullptr;
    //--
    //--added by qinyaning. 202007141028
    double _currRequestMotorRpm;
    //--
public:
	//const QString MotorSerialPort = "MotorSerialPort";


};

#endif //OCTGUI_PROTOCOL_H
